package buildings.net.server.parallel;

import buildings.Building;
import buildings.Buildings;
import buildings.dwelling.Dwelling;
import buildings.dwelling.DwellingFactory;
import buildings.dwelling.hotel.Hotel;
import buildings.dwelling.hotel.HotelFactory;
import buildings.net.server.exceptions.BuildingUnderArrestException;
import buildings.office.OfficeBuilding;
import buildings.office.OfficeFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class BinaryServerThread extends Thread {

    private Socket clientSocket;

    public BinaryServerThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    clientSocket.getInputStream()));
            PrintWriter out =
                    new PrintWriter(clientSocket.getOutputStream(), true);
            Building buf = null;
            float cost = 0;
            String type = null;
            String str = null;
            while (true) {
                type = in.readLine();
                switch (type) {
                    case "dwelling":
                        Buildings.setBuildingFactory(new DwellingFactory());
                        buf = Buildings.readBuilding(in);
                        try {
                            str = Float.toString(getCost((Dwelling) buf));
                        } catch (BuildingUnderArrestException e) {
                            str = "Здание под арестом!";
                        }
                        break;
                    case "office":
                        Buildings.setBuildingFactory(new OfficeFactory());
                        buf = Buildings.readBuilding(in);
                        try {
                            str = Float.toString(getCost((Dwelling) buf));
                        } catch (BuildingUnderArrestException e) {
                            str = "Здание под арестом!";
                        }
                        break;
                    case "hotel":
                        Buildings.setBuildingFactory(new HotelFactory());
                        buf = Buildings.readBuilding(in);
                        try {
                            str = Float.toString(getCost((Dwelling) buf));
                        } catch (BuildingUnderArrestException e) {
                            str = "Здание под арестом!";
                        }
                        break;
                }
                if (!type.equals("end")) {
                    out.println(str);
                } else {
                    break;
                }
            }

        } catch (IOException e) {
        }
    }

    private static float getCost(Dwelling b)
            throws BuildingUnderArrestException {
        if (!isArested()) {
            return b.getTotalSquare() * 1000;
        }
        throw new BuildingUnderArrestException();
    }

    private static float getCost(OfficeBuilding b)
            throws BuildingUnderArrestException {
        return b.getTotalSquare() * 1500;
    }

    private static float getCost(Hotel b)
            throws BuildingUnderArrestException {
        return b.getTotalSquare() * 2000;
    }

    private static boolean isArested() {
        Random r = new Random();
        int buf = r.nextInt(100);
        if (buf < 10) {
            return true;
        }
        return false;
    }
}
